// pages/scan/index.js
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
import { api } from '../../utils/api';
var app  = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    longitude:0,
    latitude:0,
    avatar:'/static/images/basicprofile.jpeg',
    timeSpan:0,
    user:{},
    intervalHandler:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this
    that.volunteer();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    const that = this
    if(that.data.intervalHandler) {
      clearInterval(that.data.intervalHandler);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onShareTimeline(){

  },
  volunteer() {
    const that = this
    app.api.get(app.getUrl('volunteer.info')).then((res)=>{
      that.setData({
        user:res
      })
      if(that.data.user.activity) {
        that.data.timeSpan = that.getminutes();
        that.setData({
          timeSpan:that.data.timeSpan
        })
        that.data.intervalHandler = setInterval(function() {
          that.data.timeSpan = that.getminutes();
          that.setData({
            timeSpan:that.data.timeSpan
          })
        }, 1000);
      }
    })
  },
  getminutes() {
    const that = this
    let date2 = new Date().getTime()/1000;
    let date1 = that.data.user.activity.record_time1;
    let diff = (date2 - date1);
    let hour = Math.floor(diff/3600);
    diff = diff%3600;
    let minute =  Math.floor(diff/60);
    return hour+'小时'+minute+'分';
  },
  scancode(){
    const that = this
    if(!that.data.user.volunteer) {
      that.notifyFail('请先进行志愿者注册');
      wx.switchTab({
        url: '/pages/user/index',
      })
      return ;
    }
    else if(that.data.user.volunteer.status != 2) {
      that.notifyFail('志愿者信息在审核中！')
      return ;
    }

    that.updateLocation().then(res=>{
      console.log('res', res)
      that.setData({
        longitude:res.longitude,
        latitude:res.latitude
      })
      wx.scanCode({
        onlyFromCamera: true,
        scanType:['qrCode'],
        success:(res)=>{
          console.log(res)
          that.api_postRecord(res.result);
        },
        fail:(res)=>{
          that.notifyFail('未识别到正确的活动二维码')
        }
      })
    }).catch(err=>{
      that.notifyFail('位置授权失败,请开启位置授权');
    })
 


  },

  notifySuccess(msg) {
    Notify({ 
      type: 'primary', 
      message: msg,
      // safeAreaInsetTop:true,
      onClose:()=>{
        wx.navigateBack({
          delta: 0,
        })
      }
     });
  },
  notifyFail(msg='签到失败') {
    Notify({ 
      type: 'danger', 
      message: msg,
      onClose:()=>{
  
      }
     });
  },
  api_postRecord(uuid){
    const that = this
    const data = {
      activity_uuid:uuid,
      record_latitude:that.data.latitude,
      record_longitude:that.data.longitude
    }
    app.api.post(app.getUrl('activity.apply.record'), data).then(res=>{
      console.log(res)
      that.notifySuccess(res.message);
    })
  },
  updateLocation() {
    const that = this
    return new Promise((resolve, reject) => {
      wx.getLocation({
        success: function(res) {
          console.log('promise', res)
          resolve(res)
        },
        fail:function(err) {
          reject(err)
        }
      })
    })
  }

})